package behavioralPattern.visitor.example01.visitor;

import behavioralPattern.visitor.example01.product.Candy;
import behavioralPattern.visitor.example01.product.Fruit;
import behavioralPattern.visitor.example01.product.Wine;

import java.text.NumberFormat;
import java.time.LocalDate;

/**
 * 具体访问角色
 *  -- 计价
 */
public class DiscountVisitor implements Visitor{

    // 账单日期
    private LocalDate billDate;

    public DiscountVisitor(LocalDate billDate) {
        this.billDate = billDate;
        System.out.println("结算日期：" + this.billDate);
    }

    @Override
    public void visit(Candy candy) {
        System.out.println("糖果：" + candy.getName());

        // 糖果 >180天 禁止售卖,否则一律 9 折
        long days = billDate.toEpochDay() - candy.getDate().toEpochDay();
        if (days > 180){
            System.out.println("超过半年的糖果请勿食用");
        }else {
            double realPrice = candy.getPrice() * 0.9;
            System.out.println("糖果实际价格：" + NumberFormat.getCurrencyInstance().format(realPrice));
        }
    }

    @Override
    public void visit(Wine wine) {
        System.out.println("酒类：" + wine.getName() + "不过期，无折扣！");
        System.out.println("酒水价格：" + NumberFormat.getCurrencyInstance().format(wine.getPrice()));
    }

    @Override
    public void visit(Fruit fruit) {
        System.out.println("水果：" + fruit.getName());

        // 水果 > 7天 禁止售卖
        long days = billDate.toEpochDay() - fruit.getDate().toEpochDay();

        double rate = 0;
        if (days > 7){
            System.out.println("超过7天的水果请勿食用");
        }else if (days > 3){
            rate = 0.5;
        }else {
            rate = 1.0;
        }

        double realPrice = fruit.getPrice() * rate * fruit.getWeight();

        System.out.println("水果价格：" + NumberFormat.getCurrencyInstance().format(realPrice));
    }
}
